﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AutoGear.BLL;
using ECommerce.Library.Web;

public partial class Gift_Certificates : SitePublicBasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ClearPage();
            GenerateSEOData();
        }
    }
    private void GenerateSEOData()
    {
        SEOHelper.RenderTitle(this.Page, "Gift Certificates :: Traditional Polish foods, steaks, Polish sausages, Polish dumplings, Polish pastries, Polish soups, Polish stews by Polana ", true);
        SEOHelper.RenderMetaTag(this.Page, "description", "Polana Gift Certificates. Authentic, traditional Polish sausages, kielbasa, smoked sausages, bacon, blood sausage, steaks, premade dishes, parties, torts, breads, coffees, teas, canned goods, dry goods, recipes and more from Polana.", true);
        SEOHelper.RenderMetaTag(this.Page, "keywords", "authentic, traditional, kielbasa, buy polish food, order polish food, dumplings, coffee, tea, cookies, chocolates, pastries, torts, soups, stews, European sausage, fresh polish sausage, smoked sausage, polish food, polish recipes, polish hams, polish bacons, fresh sausage, sausages, deli sausage, smoked sausages, blood sausages, headcheese, polish cheese, polish hams, polish blood sausage, polish bacon, wieners, gift certificates, offers, deals, coupons, gift card", true);
        SEOHelper.RenderMetaTag(this.Page, "robots", "ALL", true);
    }
    public void ClearPage()
    {
        tbAmount.Text = "";
        tbFromEmail.Text = "";
        tbFromName.Text = "";
        tbMessege.Text = "";
        tbQuantity.Text = "";
        tbToEmail.Text = "";
        tbToName.Text = "";
    }

    protected void ButtonSubmit_Click(object sender, EventArgs e)
    {
        string messege = string.Empty;
        double amount = Convert.ToDouble(tbAmount.Text.Trim());
        int quantity = Convert.ToInt32(tbQuantity.Text.Trim());

        if (amount > 0 && quantity > 0)
        {
            if (this.AddGiftCardToShoppingCart(
                tbFromEmail.Text.Trim(), tbFromName.Text.Trim(),
                tbToEmail.Text.Trim(), tbToName.Text.Trim(),
                tbMessege.Text.Trim(), amount, quantity, out messege))
            {
                ClearPage();
                rpGiftCertificate.SetSuccessMessage(messege);
                ButtonCheckOut.Visible = true;
            }
            else
            {
                rpGiftCertificate.SetFailMessage(messege);
            }
        }
        else
        {
            rpGiftCertificate.SetFailMessage("Incorrect quantity/amount.");
        }

    }

    private bool AddGiftCardToShoppingCart(string fromEmail, string fromName,
        string toEmail, string toName, string message, double amount, int quantity,
        out string msg)
    {
        msg = string.Empty;
        int giftCardExpiration = 3;//months
        string giftCertificateCode = Guid.NewGuid().ToString(); //GiftCertificateManager.GetGiftCertificateCode();
        GiftItem giftItem = new GiftItem(Guid.NewGuid().ToString(), giftCertificateCode, fromEmail, fromName,
            toEmail, toName, message, quantity, amount, DateTime.Now, DateTime.Now.AddMonths(giftCardExpiration));

        ShoppingCart tempShoppingCart = AutoGearSessionManager.ShoppingCart;
        tempShoppingCart.AddGiftItem(giftItem);
        AutoGearSessionManager.ShoppingCart = tempShoppingCart;
        msg = "Your gift certificate amounting " + string.Format("{0:c}", amount) + " added to shopping cart successfully.";
        return true;
        
    }

    protected void ButtonCheckOut_Click(object sender,EventArgs e)
    {
        if (AutoGearSessionManager.ShoppingCart.GiftItems.Count==0)
        {
            string messege = string.Empty;
            double amount = 0;
            if(!string.IsNullOrEmpty(tbAmount.Text.ToString()))
            amount= Convert.ToDouble(tbAmount.Text.Trim());

            int quantity = 0;
            if(!string.IsNullOrEmpty(tbQuantity.Text.Trim().ToString()))
            Convert.ToInt32(tbQuantity.Text.Trim());
            if (quantity > 0 && amount > 0)
            {
                if (this.AddGiftCardToShoppingCart(tbFromEmail.Text.Trim(), tbFromName.Text.Trim(), tbToEmail.Text.Trim(), tbToName.Text.Trim(), tbMessege.Text.Trim(), amount, quantity, out messege))
                {
                    ClearPage();
                    rpGiftCertificate.SetSuccessMessage(messege);
                }
                else
                {
                    rpGiftCertificate.SetFailMessage(messege);
                }
            }
        }

        if (SessionManager.IsUserLoggedIn)
        {
            AutoGearSessionManager.CheckoutSteps = CheckoutSteps.PreCheckout;
            Response.Redirect(CustomSiteMap.ShoppingCart);
        }
        else
        {
            AutoGearSessionManager.CheckoutSteps = CheckoutSteps.ShoppingCart;
            Response.Redirect(CustomSiteMap.ShoppingCart);
        }
    }

}